<?php
	
	/**
	 * 领取库存商品前置操作
	 */
	function drink_get_before($model){
		
		$module = M('Drink');
		$data = $module->find($model->drinkid);
		
		$model->solevar   = $data['solevar'];
		$model->title     = $data['title'];
		$model->entitle   = $data['entitle'];
		$model->sprice    = $data['sprice'];
		$model->mprice    = $data['mprice'];
		$model->addtime   = time();
		$model->drink_num = $data['drink_num'];
		
		return $model;
	}
	
	/**
	 * 领取库存商品后置操作
	 */
	function drink_get_after($table,$id,$action){
		
		$one = M($table)->find($id);
		M('Drink')->where('id='.$one['drinkid'])->setDec('num',$one['num']);
	}

	/**
 	* 添加饮品前置操作 
 	*/
	function drink_add_before($model){
		
		$title = $model->title;
		
		if($title){
			$str = mb_substr($model->title,0,1,'utf-8');
			
			$model->alpha = D('Product')->is_chinese($str);
		}
		
		return $model;
	}	
	
	/**
	 * 添加餐厅菜品前置操作
	 * @param $model
	 */
	function dishes_add_before($model){
		F("data",$model);
		
		$module = M("Dishes");
		$view   = $module->find($model->id);

		if(empty($view['solevar'])){
			$model->solevar = create_id();
		}
		
		if($_POST['flag']){
			
			//移除掉数组中值为空的数组
			$gnfr = array_remove($_POST['flag']);
			
			//对数组进行排序并重新索引
			array_multisort($gnfr);
			foreach ($gnfr as $k=>$v){
				array_multisort($gnfr[$k]['config']);
			}
			
			//将数组拼装成带有等级的字符串
			$level_str = array_level_str($gnfr);
			
			//将数组转成json字符串
			$model->gnfr  = json_encode($gnfr);
			$model->part  = $level_str;
		}else{
			$model->gnfr  = "";
			$model->part  = "";
		}
		return $model;
	}
	
	/**
	 * 移除掉数组中值为空的元素
	 */
	function array_remove($array){
		foreach ($array as $k=>$v){
			foreach ($v['config'] as $z=>$y){
				foreach($y as $m=>$n){
					if(empty($n)){
						unset($array[$k]["config"][$z][$m]);
					}
				}
				if(empty($array[$k]["config"][$z])){
					unset($array[$k]["config"][$z]);
				}
			}
			if(empty($array[$k]['config'])){
				unset($array[$k]);
			}
		}
		return $array;
	}
	
	/**
	 * 移除掉数组中值为空的元素
	 */
	function array_level_str($array){
		 
		foreach ($array as $k=>$v){
			$str = $k.":";
			foreach ($v['config'] as $z=>$y){
				$str .= $z.",";
			}
			$string .= $str."|";
		}
		return $string;
	}
	
	function address_before_deal($model){
		
		$ssxadd   = $model->ssxadd;
		$enssxadd = $model->enssxadd;
		$detail   = $model->detail;
		$endetail = $model->endetail;
		
		$model->address   = $ssxadd.','.$detail;
		$model->enaddress = $enssxadd.','.$endetail;
		
		return $model;
	}
	
	function fontadd_before_deal($model){
		
		$enssxadd = $model->enssxadd;
		$endetail = $model->endetail;
	
		$model->address   = $enssxadd.','.$endetail;
	
		return $model;
	}
	
	/**
	 * 常用地址后置操作-更新百度缓存
	 */
	function address_after_deal($table,$id,$action){
		
		$model = M("$table");
		$data  = $model->field("mapid,title,longitude,latitude,area,address,tags,tel,city,address,enaddress")->find($id);
		$data["outletid"]    = $id;
		$data['geotable_id'] = C("MAP_ADDRESS_ID");
		$data['title']       = $data['address'];
		
		//dump($data);exit;
		
		if($data["mapid"]){
			$data["id"]   = $data["mapid"];
			$json = mapStorge($data,"update");
		}else{
			$json = mapStorge($data,"create");
			$mapid= $json['id'] ;
			$model->save(array("id"=>$id,"mapid"=>$mapid));
		}
		
		if($json['status'] === 0){
			mapAll(true);
		}else{
			//$this->assign("jumpUrl",U("Outlet/edit","id=$id"));
			//$this->error("标记门店地址出错，请重新标记");
		}
	}
?>
